//
//  disCountdetailViewController.m
//  UI_HLJ_Frame
//
//  Created by  毕宏伟 on 15/7/4.
//  Copyright (c) 2015年 毕宏伟. All rights reserved.
//

#import "disCountdetailViewController.h"
#import "AFNetworking.h"
#import "discountModel.h"
#import "DisCountnewCellCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "taocanViewController.h"

#define WIDHT self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface disCountdetailViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,retain)UICollectionView *collectionView;
@property(nonatomic,retain)NSMutableArray *dicArr;
@end

@implementation disCountdetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor cyanColor];
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"My背景图片"]]];
    
    [self createData];
    
    [self createView];
    
}



- (void)createData
{
    self.dicArr =[NSMutableArray array];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://hunliji.com/v1/api/app/works/discount" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic =responseObject;
        NSArray *worksArr = dic[@"works"];
        for (NSDictionary *worksdic in worksArr) {
            
            discountModel *dis = [[discountModel alloc]init];
            [dis setValuesForKeysWithDictionary:worksdic];
            [self.dicArr addObject:dis];
            [dis release];
        }
//        NSLog(@"数据解析成功");
        [self.collectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
    
}



- (void)createView
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    //设置cell的大小
    flow.itemSize =CGSizeMake(self.view.frame.size.width,self.view.frame.size.height/1.4);
    flow.minimumInteritemSpacing =0;
    flow.minimumLineSpacing =0;
    flow.sectionInset = UIEdgeInsetsMake(0,0,0,0);
    //设置水平滚动
    [flow setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDHT, HEIGHT) collectionViewLayout:flow];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource =self;
    self.collectionView.backgroundColor = [UIColor colorWithRed:1.000 green:0.620 blue:0.903 alpha:1.000];

    [self.view addSubview:self.collectionView];
    [_collectionView release];
    
    //注册collectionviewcell
    [self.collectionView registerClass:[DisCountnewCellCollectionViewCell class] forCellWithReuseIdentifier:@"reuse11"];

}




//实现collectionview的协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dicArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    DisCountnewCellCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"reuse11" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:1.000 green:0.620 blue:0.903 alpha:1.000];
    
    discountModel *dis = self.dicArr[indexPath.row];
    [cell.bigImage sd_setImageWithURL:[NSURL URLWithString:dis.cover_path] placeholderImage:[UIImage imageNamed:@"占位图"]];
    //[cell.bigImage sd_setImageWithURL:[NSURL URLWithString:dis.cover_path]];
    cell.titleLabel.text =dis.title;
    cell.shichangLabel.text =[NSString stringWithFormat:@"市场价 :￥%@",dis.market_price];
    cell.zhekouLabel.text =[NSString stringWithFormat:@"折扣 :￥%@",dis.actual_price];
       return cell;
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    taocanViewController *taoVC = [[taocanViewController alloc]init];
    
    discountModel *dis = self.dicArr[indexPath.row];
    taoVC.bid =dis.bid;
    [self.navigationController pushViewController:taoVC animated:YES];
    
    
    [taoVC release];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
